﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ECare.Database;
using System.Windows.Forms;
using Encoder = System.Drawing.Imaging.Encoder;

namespace ECare.BUS
{
    public class UserInfoHelper
    {
        //222222
        public static int CreateUserInfo(UserInfo userinfo)
        {
            try
            {
                using (var context = new Entities())
                {
                    var newuser = new user();
                    newuser.user_un = userinfo.UserLoginName;
                    newuser.user_email = userinfo.UserEmail;
                    newuser.user_isactive = true;
                    newuser.user_phone = userinfo.UserPhone;
                    newuser.user_pw = userinfo.UserPassword;

                    newuser.user_fullname = userinfo.UserName;
                    newuser.user_roleid = userinfo.RoleId;

                    context.users.AddObject(newuser);
                    context.SaveChanges();

                    return newuser.user_id;
                }
            }
            catch (Exception ex)
            {
                return -1;
            }
        }






        public static bool UpdateUserInfo(UserInfo userinfo)
        {
            if (userinfo.UserId == 0)
                return false;

            using (var context = new Entities())
            {
                var finduser = context.users.FirstOrDefault(a => a.user_id == userinfo.UserId);
                if (finduser == null)
                    return false;

                finduser.user_un = userinfo.UserName;
                finduser.user_email = userinfo.UserEmail;
                finduser.user_isactive = true;
                finduser.user_phone = userinfo.UserPhone;
                finduser.user_pw = userinfo.UserPassword;

                finduser.user_fullname = userinfo.UserName;
                finduser.user_roleid = userinfo.RoleId;

                context.SaveChanges();

                return true;
            }
        }

        public static List<UserInfo> GetUsers()
        {
            using (var context = new Entities())
            {
                var users = context.users;
                var res = new List<UserInfo>();
                foreach (var user in users)
                {
                    var newuser = new UserInfo();
                    newuser.UserId = user.user_id;
                    newuser.UserLoginName = user.user_un;
                    newuser.UserName = user.user_fullname;
                    newuser.UserPassword = user.user_pw;
                    newuser.UserPhone = user.user_phone;
                    newuser.RoleId = user.user_roleid.HasValue ? user.user_roleid.Value : 0;
                    newuser.UserEmail = user.user_email;
                    newuser.IsActive = user.user_isactive.HasValue ? user.user_isactive.Value : true;
                    res.Add(newuser);
                }
                return res;
            }
        }

        public static string GetUserNameById(int UserId)
        {
            using (var context = new Entities())
            {
                var finduser = context.users.FirstOrDefault(a => a.user_id == UserId);
                if (finduser == null)
                {
                    return "未知操作员";
                }
                else
                {
                    return finduser.user_fullname;
                }
            }
        }
        //***********************************************************************************************
        /// <summary>
        /// 
        /// </summary>
        /// <param name="userinfo"></param>
        /// <returns></returns>
        public static bool DeleteUserInfo(UserInfo userinfo)
        {
            try
            {
                using (var context = new Entities())
                {
                    var finduser = context.users.FirstOrDefault(a => a.user_id == userinfo.UserId);
                    if (finduser == null)
                    {
                        return false;
                    }
                    else
                    {
                        DialogResult r1 = MessageBox.Show("请确认是否要删除该项数据", "", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                        if (r1 == DialogResult.Yes)
                        {
                            context.users.DeleteObject(finduser);
                            context.SaveChanges();
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }




        //******************************************************************
        //Function: Check the uniqueness of username 
        //Author: wxw
        //Date: 2014/09/04
        public static bool CheckUserLoginName(UserInfo userinfo)
        {
            try
            {
                using (var context = new Entities())
                {
                    var finduser = context.users.FirstOrDefault(a => a.user_un == userinfo.UserLoginName);
                    if (finduser == null)
                        return true;
                    else
                        return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        //******************************************************************
        //Function: Check the correctness of username and password 
        //Author: wxw
        //Date: 2014/09/04
        //
        public static bool CheckUser(UserInfo userinfo)
        {
            try
            {
                using (var context = new Entities())
                {
                    var finduser = context.users.FirstOrDefault(a => (a.user_un == userinfo.UserLoginName
                        && a.user_pw == userinfo.UserPassword
                        && a.user_isactive == true  // only the actived user can login in the system
                        ));
                    if (finduser == null)
                        return false;
                    else
                    {
                        userinfo.UserId = finduser.user_id;
                        userinfo.RoleId = finduser.user_roleid.Value;
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {

                throw ex;
                return false;
            }
        }

    }
}
